<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="com.example.utils.DButil" %>
<%@ page import="com.example.dao.impl.UserDaoimpl" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="com.example.entity.UserImage" %>
<%@ page import="com.example.entity.User" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="java.sql.Connection" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport"
		  content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
	<title>用户</title>
	<link href='https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css' rel='stylesheet'><!-- 图标 -->

	<link rel="preconnect" href="https://fonts.googleapis.com">
	<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
	<link
			href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap"
			rel="stylesheet"><link rel="stylesheet" href="./css/移动端页面功能.css">

</head>
<style>
	*{
		box-sizing: border-box;
	}
	img{
		width: 50px;
		height: 50px;
		object-fit: cover;
	}
	.EditorTd{
		display: flex;
		align-content: center;
		justify-content: left;
	}
	p{
		margin: 0px;
		padding: 0px;
		padding-left: 20px;
	}
	/*b{*/
	/*	position: absolute;*/
	/*	right: 10px;*/
	/*}*/

	button {
		position: absolute;
		right: 10px;
		background: hotpink;
		color: #fff;
		border: 0px;
		cursor: pointer;
		width: 100px;
		height: 40px;
		border-radius: 10px;
	}

	.qiandao {}

	.qiandao.active {
		background: rgba(0, 0, 0, 0.02);
		border: 1px solid #000;
		color: #000;
	}
</style>
<body>
<header>
	<h2 style="text-align: center;padding-left: 0px;">用户</h2>
</header>
<table style="height: 70vh; overflow-y: auto;">
	<%
		User user = (User) session.getAttribute("user");
		Connection con = DButil.getConnection();
		PreparedStatement pst = null;
		PreparedStatement pst2 = null;
		PreparedStatement pst3 = null;
		ResultSet rs = null;
		ResultSet rs2 = null;
		try {
			pst = con.prepareStatement("select * from userinfo");
			pst2 = con.prepareStatement("select * from friend");
			rs = pst.executeQuery();
			rs2 = pst2.executeQuery();

			UserDaoimpl userDaoimpl = new UserDaoimpl();

			List<Integer> list = new ArrayList<>();
			List<Integer> list2 = new ArrayList<>();
			List<Integer> list3 = new ArrayList<>();
			while(rs2.next()){
				list3.add(rs2.getInt("USERID"));
				list2.add(rs2.getInt("FRIENDID"));
				list.add(rs2.getInt("GUANZHU"));
			}
			while (rs.next()){
				if (rs.getInt("USER_ID")==user.getUserId()){
					continue;
				}
				UserImage userImage = userDaoimpl.findByUserID(rs.getInt("USER_ID"));

				out.print("<tr>");
				out.print("<td class='EditorTd'>");
				out.print("<img src="+userImage.getUserImage()+" alt='' />");
				out.print("<div class='s'>");
				out.print("<p style='display:none'><b>"+rs.getString("USER_ID")+"</b></p>");
				out.print("<p><b>"+rs.getString("USERNAME")+"</b></p>");
//				out.print("<p>ll</p>");
				out.print("</div>");
				boolean flag = false;
				for (int i = 0; i < list.size(); i++) {
					if (list.get(i) == 1 && list2.get(i) == rs.getInt("USER_ID") && list3.get(i) == user.getUserId()){
						flag = true;
					}
				}
				if (flag){
					pst3 = con.prepareStatement("INSERT INTO liaotian(USERID1,USERID2,CONTENT) VALUES(?,?,'Hello')");
					pst3.setInt(1,user.getUserId());
					pst3.setInt(2,rs.getInt("USER_ID"));
					out.print("<button class='qiandao active'>已关注</button>");
				}else {
					out.print("<button class='qiandao'>关注</button>");
				}

				out.print("<hr>");
				out.print("</td>");
				out.print("</tr>");
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}finally {
			DButil.closeResources(rs,pst,rs2,pst2,pst3,con);
		}


	%>
</table>

</body>
<script src="./js/jquery.min.js"></script>
<script>
	var qiandao = document.getElementsByClassName('qiandao');
	var number = 0;
	for (let i = 0; i < qiandao.length; i++) {
		qiandao[i].addEventListener('click', function() {
			qiandao[i].classList.toggle('active');
			if (qiandao[i].classList.contains('active')) {
				qiandao[i].textContent = "已关注";
				number = 1;
			} else {
				qiandao[i].textContent = "关注";
				number = 0;
			}
		});
	}

	var qiandaos = document.querySelectorAll('.qiandao');
	qiandaos.forEach(function (item){
		item.addEventListener('click',function(){
			var userid = item.parentNode.children[1].children[0].innerText;
			console.log(userid)

			$.ajax({
				url: "sendtoguanzhu.jsp",
				type: "POST",
				data: {
					userid: ${user.getUserId()},
					guanzhuid:userid,
					number:number
				},
			});
		})
	});



</script>
</html>